package com.gzqylc.docker.extend.auth.controller;

import com.gzqylc.docker.extend.auth.bo.LoginBO;
import com.gzqylc.docker.extend.auth.bo.UserChangeBO;
import com.gzqylc.docker.extend.auth.domain.BaseUser;
import com.gzqylc.docker.extend.auth.service.BaseUserService;
import com.gzqylc.framework.AjaxResult;
import com.gzqylc.framework.security.LoginSession;
import com.gzqylc.lang.web.RequestTool;
import com.gzqylc.lang.web.jwt.Jwt;
import com.gzqylc.lang.web.jwt.JwtPayload;
import com.gzqylc.lang.web.jwt.JwtTool;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录验证
 */
@RestController
@RequestMapping("/api/login/")
public class LoginController {

    @Autowired
    BaseUserService baseUserService;

    @RequestMapping("/account")
    public AjaxResult ajaxLogin(HttpServletRequest request,@Valid @RequestBody LoginBO login) {
        String loginName = login.getLoginName();
        String inputPassword = login.getPassword();

        return AjaxResult.success("登录成功",baseUserService.login(request,loginName,inputPassword));
    }

    @RequestMapping("/page")
    public Page<BaseUser> page(BaseUser baseUser, Pageable pageable){
        return baseUserService.page(baseUser, pageable);
    }

    @RequestMapping("/update")
    public AjaxResult update(@RequestBody UserChangeBO bo){
        baseUserService.update(bo);
        return AjaxResult.success();
    }

    @RequestMapping("/delete")
    public AjaxResult delete(@RequestParam String userId){
        baseUserService.delete(userId);
        return AjaxResult.success();
    }
    @RequestMapping("logout")
    public AjaxResult logout() {
        HttpServletRequest request = RequestTool.currentRequest();
        if(request == null) {
            return AjaxResult.success("注销成功");
        }
        String jwt = JwtTool.getJwt(request);
        baseUserService.logout(jwt);
        return AjaxResult.success("注销成功");
    }

}
